开始之前:

1. 识别画面中的所有表情

在表情识别功能下,HUSKYLENS 2 能识别7种特定的表情:愤怒(ID为1)、厌恶(ID为2)、恐惧(ID为3)、开心(ID为4)、中性(ID为5)、悲伤(ID为6)和惊讶(ID为7)。以上表情已经在HUSKYLENS 2 出厂时学习完毕,无需用户再次手动学习,表情识别的详细功能使用说明请见表情识别功能说明

通过以下示例程序能统计当前HUSKYLENS 2 摄像头画面出识别出的所有表情的数量,并依次输出各个表情的相关数据。

示例程序如下

#include "DFRobot_HuskylensV2.h"

// 动态变量
volatile float mind_n_BiaoQingZongShu, mind_n_BiaoShiBianLiang;
// 创建对象
HuskylensV2 huskylens;


// 主程序开始
void setup() {
	Serial.begin(9600);
	Wire.begin();
  while (!huskylens.begin(Wire)) {
    delay(100);
  }
	mind_n_BiaoQingZongShu = 0;
	mind_n_BiaoShiBianLiang = 0;
}
void loop() {
	huskylens.getResult(ALGORITHM_EMOTION_RECOGNITION);
	if ((huskylens.available(ALGORITHM_EMOTION_RECOGNITION))) {
		mind_n_BiaoQingZongShu = (huskylens.getCachedResultNum(ALGORITHM_EMOTION_RECOGNITION));
		Serial.println((String("画面中的表情总数为:") + String(mind_n_BiaoQingZongShu)));
		while (!(mind_n_BiaoShiBianLiang>=mind_n_BiaoQingZongShu)) {
			mind_n_BiaoShiBianLiang += 1;
			Serial.println((String((String("第") + String((String(mind_n_BiaoShiBianLiang) + String("个表情为:"))))) + String((RET_ITEM_STR(huskylens.getCachedResultByIndex(ALGORITHM_EMOTION_RECOGNITION, mind_n_BiaoShiBianLiang-1), Result, name)))));
		}
		mind_n_BiaoShiBianLiang = 0;
	}
	delay(500);
}

当任意以上七种表情出现在摄像头画面中,观察HUSKYLENS 2 的屏幕会将表情框出,显示表情名称和置信度。

**运行结果:**观察串口输出当前识别到的表情总数和对应名称。

Interface Diagram

2. 统计每类表情数量

在已知表情识别功能下输出的ID只有数字1——7(对应7种表情)的情况下,我们可以使用以下示例程序统计当前HUSKYLENS 2 摄像头画面出识别出的每一类表情的数量。


#include "DFRobot_HuskylensV2.h"

// 动态变量
volatile float mind_n_BiaoShiBianLiang;
// 创建对象
HuskylensV2 huskylens;


// 主程序开始
void setup() {
	Serial.begin(9600);
	Wire.begin();
  while (!huskylens.begin(Wire)) {
    delay(100);
  }
	huskylens.switchAlgorithm(ALGORITHM_EMOTION_RECOGNITION);
	mind_n_BiaoShiBianLiang = 1;
}
void loop() {
	huskylens.getResult(ALGORITHM_EMOTION_RECOGNITION);
	if ((huskylens.available(ALGORITHM_EMOTION_RECOGNITION))) {
		if ((mind_n_BiaoShiBianLiang<=7)) {
			if (((huskylens.getCachedResultByID(ALGORITHM_EMOTION_RECOGNITION, mind_n_BiaoShiBianLiang) != NULL))) {
				Serial.println((String((String((RET_ITEM_STR(huskylens.getCachedResultByID(ALGORITHM_EMOTION_RECOGNITION, mind_n_BiaoShiBianLiang), Result, name))) + String("的总数为:"))) + String((huskylens.getCachedResultNumByID(ALGORITHM_EMOTION_RECOGNITION, mind_n_BiaoShiBianLiang)))));
			}
			mind_n_BiaoShiBianLiang += 1;
		}
		else {
			mind_n_BiaoShiBianLiang = 1;
		}
	}
	delay(500);
}

**运行结果:**如下,运行程序,当画面中出现多张不同表情的人脸图像时,观察串口输出当前识别到的每一类表情总数的名称和对应数量。

Interface Diagram